package com.ruoyi.APPMS.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.APPMS.domain.Supplier;
import com.ruoyi.APPMS.service.ISupplierService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商管理Controller
 *
 * @author wanghaoyu
 * @date 2023-07-02
 */
@RestController
@RequestMapping("/supplier/supplier")
@Api(tags = "供应商管理控制器")
public class SupplierController extends BaseController {
    @Autowired
    private ISupplierService supplierService;

    /**
     * 查询供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:supplier:list')")
    @GetMapping("/list")
    @ApiOperation("分页查询供应商信息")
    public TableDataInfo list(Supplier supplier) {
        startPage();
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        if (supplier.getSuppliername() != null) {
            queryWrapper.like("suppliername", supplier.getSuppliername());
        }
        if (supplier.getContactperson() != null) {
            queryWrapper.like("contactperson", supplier.getContactperson());
        }
        if (supplier.getContactnumber() != null) {
            queryWrapper.like("contactnumber", supplier.getContactnumber());
        }
        if (supplier.getAddress() != null) {
            queryWrapper.like("address", supplier.getAddress());
        }
        List<Supplier> list = supplierService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:supplier:export')")
    @Log(title = "供应商管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出供应商信息")
    public void export(HttpServletResponse response, Supplier supplier) {
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        if (supplier.getSuppliername() != null) {
            queryWrapper.like("suppliername", supplier.getSuppliername());
        }
        List<Supplier> list = supplierService.list(queryWrapper);
        ExcelUtil<Supplier> util = new ExcelUtil<Supplier>(Supplier.class);
        util.exportExcel(response, list, "供应商管理数据");
    }

    /**
     * 获取供应商管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('supplier:supplier:query')")
    @GetMapping(value = "/{supplierid}")
    @ApiOperation("展示一条供应商信息")
    public AjaxResult getInfo(@PathVariable("supplierid") Long supplierid) {
        return success(supplierService.getById(supplierid));
    }

    /**
     * 新增供应商管理
     */
    @PreAuthorize("@ss.hasPermi('supplier:supplier:add')")
    @Log(title = "供应商管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加供应商信息")
    public AjaxResult add(@RequestBody Supplier supplier) {
        supplier.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(supplierService.save(supplier));
    }

    /**
     * 修改供应商管理
     */
    @PreAuthorize("@ss.hasPermi('supplier:supplier:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改供应商信息")
    public AjaxResult edit(@RequestBody Supplier supplier) {
        supplier.setModifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(supplierService.updateById(supplier));
    }

    /**
     * 删除供应商管理
     */
    @PreAuthorize("@ss.hasPermi('supplier:supplier:remove')")
    @Log(title = "供应商管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supplierids}")
    @ApiOperation("删除供应商信息")
    public AjaxResult remove(@PathVariable List<Long> supplierids) {
        return toAjax(supplierService.removeByIds(supplierids));
    }

    @PreAuthorize("@ss.hasPermi('supplier:supplier:getlistSuppliers')")
    @Log(title = "查询所有供应商数据，初始化下拉列表", businessType = BusinessType.DELETE)
    @GetMapping("/getlistSuppliers")
    @ApiOperation("查询所有供应商信息")
    public AjaxResult getlistSuppliers() {
        return AjaxResult.success(supplierService.list());
    }
}
